<script setup lang="ts">
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userLogin } from '@/api/user'

const token = localStorage.getItem('milk_mamage_token')
const router = useRouter()
if (token) {
  router.push({
    name: 'customerMessage'
  })
}

const loginForm = reactive({
  name: '',
  password: ''
})
const loginFormRules = reactive({
  name: [{ required: true, message: 'Please input Activity name', trigger: 'blur' }],
  password: [{ required: true, message: 'Please input Activity name', trigger: 'blur' }]
})

const login = async () => {
  try {
    const { token } = await userLogin({ ...loginForm })
    localStorage.setItem('milk_mamage_token', token)
    router.push({
      name: 'customerMessage'
    })
    ElMessage.success('登录成功')
  } catch (error) {
    ElMessage.error((error as Error).message)
  }
}

</script>
<template>
  <div class="login_container">
    <div class="login_box">
      <div class="header">Login</div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="name">
          <el-input v-model="loginForm.name" placeholder="账号" ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btn">
          <el-button class="btn-login" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>

.login_container {
  height: 100%;
  width: 100%;
  position: relative;
  background-image: linear-gradient(to right, #330867, #30cfd0);
}
.login_box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .header {
    font-size: 30px;
    font-weight: bold;
    text-align: center;
    line-height: 140px;
  }

  .login_form {
    position: absolute;
    bottom: 40px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btn {
    margin: 10px auto;
    box-sizing: border-box;
    width: 360px;
    &-login {
      height: 36px;
      padding: 10px;
      width: 100%;
      text-align: center;
      background-image: linear-gradient(to right, #30cfd0, #330867);
      color: #fff;
    }
  }
}
</style>
